package com.sitanyun.merchant.guide.frament.view.fragment;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.HistoryUserAdapter;
import com.sitanyun.merchant.guide.base.BaseFragment;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.CallPhonesBean;
import com.sitanyun.merchant.guide.bean.FiltrateBean;
import com.sitanyun.merchant.guide.bean.ForgetsmscoedeBean;
import com.sitanyun.merchant.guide.bean.HistoryTodayBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.frament.presenter.impl.TodayUserFPresenterImpl;
import com.sitanyun.merchant.guide.frament.view.activity.TrajeActivity;
import com.sitanyun.merchant.guide.frament.view.inter.ITodayUserFView;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.view.fragment.SeeImagesFramgment;
import com.sitanyun.merchant.guide.weiht.DateUtils;
import com.sitanyun.merchant.guide.weiht.FlowPopWindow;
import com.sitanyun.merchant.guide.weiht.PhoneUtils;
import com.sitanyun.merchant.guide.weiht.ShareLogin;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.sitanyun.merchant.guide.weiht.SpaceItemDecoration;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuikit.tuicontact.bean.ContactItemBean;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 我的用户
 * A simple {@link Fragment} subclass.
 */
public class TodayUserFragment extends BaseFragment implements ITodayUserFView {


    @BindView(R.id.today_state)
    TextView todayState;
    @BindView(R.id.today_userintention)
    TextView todayUserintention;
    @BindView(R.id.today_timestort)
    TextView todayTimestort;
    @BindView(R.id.today_screen)
    ImageView todayScreen;
    @BindView(R.id.today_rv)
    RecyclerView todayRv;
    @BindView(R.id.state_img)
    ImageView stateImg;
    @BindView(R.id.userintention_img)
    ImageView userintentionImg;
    @BindView(R.id.timestort_img)
    ImageView timestortImg;
    @BindView(R.id.ll_todayone)
    LinearLayout llTodayone;
    @BindView(R.id.ll_todaytwo)
    LinearLayout llTodaytwo;
    @BindView(R.id.sw_today)
    SwipeRefreshLayout swToday;
    private TodayUserFPresenterImpl mITodayUserFPresenter;
    private String secretaec;
    private HistoryUserAdapter historyUserAdapter;
    private PopupWindow popupWindow;
    private HistoryTodayBean historyTodayBean;
    private FlowPopWindow flowPopWindow;
    private List<FiltrateBean> dictList = new ArrayList<>();
    private String havePhone = "ALL";
    private String phonesq = "-1";

    @Override
    public int getLayoutId() {
        return R.layout.fragment_today_user;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mITodayUserFPresenter = new TodayUserFPresenterImpl(this);
           initparam();
        //刷新
        swToday.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //未登录
        if ( ShareLogin.getStringData("logins") .equals("1")) {
            SeeImagesFramgment seeImagesFramgment=new SeeImagesFramgment();
            seeImagesFramgment.show(getChildFragmentManager(),"fragment");
            ShareLogin.SaveData("logins","2");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");

    }

    //筛选
    private void initparam() {
        dictList.clear();
        String[] sexs = {"已授权", "未授权"};
        String[] colors = {"发起同屏", "要求回电", "发起留言"};
        FiltrateBean fb1 = new FiltrateBean();
        fb1.setTypeName("手机号授权");
        List<FiltrateBean.Children> childrenList = new ArrayList<>();
        for (int x = 0; x < sexs.length; x++) {
            FiltrateBean.Children cd = new FiltrateBean.Children();
            cd.setValue(sexs[x]);
            childrenList.add(cd);
        }
        fb1.setChildren(childrenList);

        FiltrateBean fb2 = new FiltrateBean();
        fb2.setTypeName("互动状态");
        List<FiltrateBean.Children> childrenList2 = new ArrayList<>();
        for (int x = 0; x < colors.length; x++) {
            FiltrateBean.Children cd = new FiltrateBean.Children();
            cd.setValue(colors[x]);
            childrenList2.add(cd);
        }
        fb2.setChildren(childrenList2);

        dictList.add(fb1);
        dictList.add(fb2);
    }

    @Override
    public void initView() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        todayRv.setLayoutManager(layoutManager);
        historyUserAdapter = new HistoryUserAdapter(getActivity());
        //设置item间距，30dp
        todayRv.addItemDecoration(new SpaceItemDecoration(20));
        todayRv.setAdapter(historyUserAdapter);
        historyUserAdapter.notifyDataSetChanged();

        //点击跳转
        historyUserAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    //轨迹
                    case R.id.usertraje:
                        if (DateUtils.isFastClick()) {
                            return ;
                        }
                        startActivity(new Intent(getActivity(), TrajeActivity.class)
                                .putExtra("scenid", historyTodayBean.getData().get(position).getCallSceneId() + "")
                                .putExtra("cid", historyTodayBean.getData().get(position).getCId() + "")
                                .putExtra("callnoid", historyTodayBean.getData().get(position).getCallNoticeId() + "")
                                .putExtra("scenid", historyTodayBean.getData().get(position).getCallSceneId() + ""));
                        break;
                        //留言
                    case R.id.interactive:
                        if (DateUtils.isFastClick()) {
                            return ;
                        }
                        Bundle bundle = new Bundle();
                        bundle.putString(TUIConstants.TUIChat.CHAT_ID, "cid_" + historyTodayBean.getData().get(position).getCId());
                       SharedPreferenceUtil.SaveData("interactive","cid_" + historyTodayBean.getData().get(position).getCId());
                        SharedPreferenceUtil.SaveData("cid",historyTodayBean.getData().get(position).getCId() + "");
                        bundle.putString(TUIConstants.TUIChat.CHAT_NAME, historyTodayBean.getData().get(position).getCName());
                        bundle.putInt(TUIConstants.TUIChat.CHAT_TYPE, ContactItemBean.TYPE_C2C);
                        TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, bundle);
                        initcallread("MESSAGE",historyTodayBean.getData().get(position).getCId()+"");
                        break;
                    //打电话
                    case R.id.callphone:
                        if (DateUtils.isFastClick()) {
                            return ;
                        }
                        if (historyTodayBean.getData().get(position).getCallNoticeId() == null) {
                            ToastUtil.showToast(getActivity(), "该用户无要求回电信息");
                            return;
                        } else {
                            //电话状态
                        initcallread("CALL",historyTodayBean.getData().get(position).getCId()+"");
                            initpopuview(historyTodayBean.getData().get(position).getCallNoticeId() + "");
                            historyUserAdapter.notifyDataSetChanged();
                        }
                        break;
                    //星级
                    case R.id.rb:
//              com.sitanyun.merchant.guide.weiht.RatingBar    ratingBar=(com.sitanyun.merchant.guide.weiht.RatingBar) view.findViewById(R.id.rb);
//              ratingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
//                     @Override
//                     public void onRatingChange(float ratingCount) {
//                         ToastUtil.showToast(getActivity(),"13");
//                     }
//                 });
                    break;
                }
            }
        });
    }
   //电话状态
    private void initcallread(String type , String cid) {
        Map<String, String> hashMap= new HashMap<>();
//        List lists= new ArrayList<>();
//        lists.add(bicid);
//        hashMap.put("clientTypeId",lists);
        hashMap.put("behavior",type);
 //       hashMap.put("from",lists);
        hashMap.put("to",cid);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type","application/json")
                .addHeader("Authorization","Bearer "+ SharedPreferenceUtil.getStringData("token"))
                .url(Urls.setBehaviorRead)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ForgetsmscoedeBean baseEntity=   new Gson().fromJson(response, ForgetsmscoedeBean.class);
                if (baseEntity.getCode()==0){

                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);

    }
    //弹窗电话
    // 弹窗consumerId
    private void initpopuview(String cid) {
        OkHttpUtils.get().url(Urls.getConsumerInfo)
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addHeader("Content-Type", "application/json")
                .addParams("noticeId", cid)
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {
                return;
            }

            @Override
            public void onResponse(String response, int id) {
                CallPhonesBean callPhonesBean = new Gson().fromJson(response, CallPhonesBean.class);
                if (callPhonesBean.getCode() == 0) {
//1、使用Dialog、设置style
                    final Dialog dialog = new Dialog(getActivity(), R.style.DialogTheme);
                    //2、设置布局
                    View view = View.inflate(getActivity(), R.layout.callphone_item, null);
                    dialog.setContentView(view);
                    ImageView headerimg = view.findViewById(R.id.popu_headerimg);
                    TextView headername = view.findViewById(R.id.popu_headername);
                    TextView headerphone = view.findViewById(R.id.popu_headerphone);
                    ImageView enter_img = view.findViewById(R.id.enter_img);
                    TextView enter_name = view.findViewById(R.id.enter_store);
                    enter_name.setText(callPhonesBean.getData().getSceneName());
                    TextView entershar = view.findViewById(R.id.enter_shar);
                    TextView enter_bw = view.findViewById(R.id.enter_bw);
                    enter_bw.setText("同屏讲解：" + callPhonesBean.getData().getMeetingNum() + "     留言：" + callPhonesBean.getData().getMessageNum());
                    String times = callPhonesBean.getData().getLastBehaviorTime();
                    String intTime = callPhonesBean.getData().getStartTime()+"";
                    entershar.setText("要求回电时间："+intTime.substring(0,11));
//                    int h = intTime / 3600;
//                    int m = (intTime % 3600) / 60;
//                    int s = (intTime % 3600) % 60;
//                    if (h == 0) {
//                        entershar.setText(times.substring(11, times.length()) + "浏览   共" + m + "时" + s + "秒");
//                    } else {
//                        entershar.setText(times.substring(11, times.length()) + "浏览   共" + m + "时" + s + "秒");
//                    }
                    Glide.with(getActivity()).load(callPhonesBean.getData().getPicUrl()).into(enter_img);
                    ImageView stars = view.findViewById(R.id.popu_headerstars);
                    ImageView starss = view.findViewById(R.id.popu_headerstarss);
                    ImageView starsss = view.findViewById(R.id.popu_headerstarsss);
                    if (callPhonesBean.getData().getCName()==null){
                        headername.setText("访客");
                    }else {
                        headername.setText(callPhonesBean.getData().getCName());
                    }
                    headerphone.setText(callPhonesBean.getData().getPhone());


                    switch (callPhonesBean.getData().getStars()) {
                        case 0:
                            Glide.with(getActivity()).load(R.mipmap.xwei).into(stars);
                            Glide.with(getActivity()).load(R.mipmap.xwei).into(starss);
                            Glide.with(getActivity()).load(R.mipmap.xwei).into(starsss);
                            break;
                        case 1:
                            Glide.with(getActivity()).load(R.mipmap.xxuan).into(stars);
                            Glide.with(getActivity()).load(R.mipmap.xwei).into(starss);
                            Glide.with(getActivity()).load(R.mipmap.xwei).into(starsss);
                            break;
                        case 2:
                            Glide.with(getActivity()).load(R.mipmap.xxuan).into(stars);
                            Glide.with(getActivity()).load(R.mipmap.xxuan).into(starss);
                            Glide.with(getActivity()).load(R.mipmap.xwei).into(starsss);
                            break;
                        case 3:
                            Glide.with(getActivity()).load(R.mipmap.xxuan).into(stars);
                            Glide.with(getActivity()).load(R.mipmap.xxuan).into(starss);
                            Glide.with(getActivity()).load(R.mipmap.xxuan).into(starsss);
                            break;
                    }
                    Glide.with(getActivity()).load(callPhonesBean.getData().getHeadUrl()).error(R.mipmap.cimg).into(headerimg);


                    Window window = dialog.getWindow();
                    //设置弹出位置
                    window.setGravity(Gravity.CENTER);
                    //设置弹出动画
                    //   window.setWindowAnimations(R.style.main_menu_animStyle);
                    //设置对话框大小
                    window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    dialog.show();

                    dialog.findViewById(R.id.callphones).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            PhoneUtils.callPhone(getActivity(), callPhonesBean.getData().getPhone());
                            dialog.dismiss();
                        }
                    });

                } else {
                    ToastUtil.showToast(getActivity(), "请求数据错误");
                    return;
                }
            }
        });
        mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");
        historyUserAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.today_timestort, R.id.today_state, R.id.today_userintention, R.id.today_screen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //状态
            case R.id.today_state:
                stateImg.animate().setDuration(500).rotation(180).start();
                initpopu(view);
                break;
            //用户
            case R.id.today_userintention:
                userintentionImg.animate().setDuration(500).rotation(180).start();
                initpopuuser(view);
                break;
            //时间
            case R.id.today_timestort:
                timestortImg.animate().setDuration(500).rotation(180).start();
                initpoputime(view);

                break;
            //筛选
            case R.id.today_screen:
                initpopus();

                break;
        }
    }

    //侧滑弹窗
    private void initpopus() {
        flowPopWindow = new FlowPopWindow(getActivity(), dictList);
        // flowPopWindow.showAsDropDown(ivBack);
        backgroundAlpha(0.5f);
        flowPopWindow.showAtLocation(todayScreen, Gravity.RIGHT | Gravity.CENTER_HORIZONTAL, 0, 0);

//        flowPopWindow.setOndismissClickListener(new FlowPopWindow.OndismissClickListener() {
//            @Override
//            public void OndismissClick() {
//                mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");   }
//        });
        flowPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        flowPopWindow.setOndismissClickListener(new FlowPopWindow.OndimissClickListener() {
            @Override
            public void onDismissClick() {
                phonesq = "-1";
                havePhone = "ALL";
            }
        });

        flowPopWindow.setOnConfirmClickListener(new FlowPopWindow.OnConfirmClickListener() {
            @Override
            public void onConfirmClick() {

                for (FiltrateBean fb : dictList) {
                    List<FiltrateBean.Children> cdList = fb.getChildren();
                    for (int x = 0; x < cdList.size(); x++) {
                        FiltrateBean.Children children = cdList.get(x);
                        if (children.isSelected()) {
                            //互动状态
                            if (children.getValue().equals("发起同屏")) {
                                havePhone = "MEETING";
                            }
                            if (children.getValue().equals("要求回电")) {
                                havePhone = "CALL";
                            }
                            if (children.getValue().equals("发起留言")) {
                                havePhone = "MESSAGE";
                            }
                            //手机号授权状态
                            if (children.getValue().equals("已授权")) {
                                phonesq = "1";
                            }
                            if (children.getValue().equals("未授权")) {
                                phonesq = "0";
                            }

                        }
                    }
                }
                mITodayUserFPresenter.gettodayuser("-1", "-1", "BROWSE_SECONDS", phonesq, havePhone);
            }
        });
    }

    //时间
    private void initpoputime(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.popu_times, null);
        Button times_all = (Button) contentView.findViewById(R.id.times_all);
        Button shars_all = (Button) contentView.findViewById(R.id.shars_all);
        //按来访时间
        times_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayTimestort.setText("最新来访时间");
                ///    Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //按浏览时长
        shars_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayTimestort.setText("近一日浏览时长");
                ///     Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "-1", "BROWSE_SECONDS", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });


        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                timestortImg.animate().setDuration(500).rotation(0).start();
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int xPos = view.getWidth() / 2 - popupWindow.getContentView().getMeasuredWidth() / 2;
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view, xPos, 3);
        //   popupWindow.showAtLocation(todayState, Gravity.BOTTOM,0,0);
        //  Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
    }

    //意向
    private void initpopuuser(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.popu_userintent, null);
        Button user_all = (Button) contentView.findViewById(R.id.user_all);
        Button high_intention = (Button) contentView.findViewById(R.id.high_intention);
        Button in_intention = (Button) contentView.findViewById(R.id.in_intention);
        Button low_intention = (Button) contentView.findViewById(R.id.low_intention);
        Button first_intention = (Button) contentView.findViewById(R.id.first_intention);
        //全部
        user_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayUserintention.setText("全部");
                ///    Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //高意向
        high_intention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayUserintention.setText("高意向");
                ///     Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "3", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //中意向
        in_intention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayUserintention.setText("中意向");
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "2", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //低意向
        low_intention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayUserintention.setText("低意向");
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //初次来访
        first_intention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayUserintention.setText("初次来访");
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "0", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });


        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                userintentionImg.animate().setDuration(500).rotation(0).start();
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int xPos = view.getWidth() / 2 - popupWindow.getContentView().getMeasuredWidth() / 2;
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view, xPos, 3);
        //   popupWindow.showAtLocation(todayState, Gravity.BOTTOM,0,0);
        //  Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
    }

    //状态
    private void initpopu(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.popu_state, null);
        Button alls = (Button) contentView.findViewById(R.id.alls);
        Button online = (Button) contentView.findViewById(R.id.online);
        Button ofline = (Button) contentView.findViewById(R.id.ofline);

        //分享数量
        alls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayState.setText("全部");
                ///    Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //在线
        online.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayState.setText("在线");
                ///     Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("1", "-1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });
        //离线
        ofline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayState.setText("离线");
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                mITodayUserFPresenter.gettodayuser("0", "-1", "VISIT_TIME", "-1", "ALL");
                historyUserAdapter.notifyDataSetChanged();
            }
        });

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                stateImg.animate().setDuration(500).rotation(0).start();
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int xPos = view.getWidth() / 2 - popupWindow.getContentView().getMeasuredWidth() / 2;
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view, xPos, 3);
        //   popupWindow.showAtLocation(todayState, Gravity.BOTTOM,0,0);
        //  Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
    }

    @Override
    public <T> T request(int requestFlag) {
        swToday.setRefreshing(false);
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {
        swToday.setRefreshing(false);
        if (responseFlag == ITodayUserFView.RESPONSE_ONE) {
            historyTodayBean = (HistoryTodayBean) response;
            if (historyTodayBean.getCode() == 0) {
                if (historyTodayBean.getData().size() == 0) {
                    llTodayone.setVisibility(View.GONE);
                    llTodaytwo.setVisibility(View.VISIBLE);
                } else {
                    llTodayone.setVisibility(View.VISIBLE);
                    llTodaytwo.setVisibility(View.GONE);
                    historyUserAdapter.setNewData(historyTodayBean.getData());
                    historyUserAdapter.notifyDataSetChanged();
                }


            } else {
                ToastUtil.showToast(getActivity(), historyTodayBean.getMsg() + "");
            }

        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getActivity().getWindow().setAttributes(lp);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String event) {
        Log.e("TAGSSS",event);
        if (event.equals("1")){
            mITodayUserFPresenter.gettodayuser("-1", "-1", "VISIT_TIME", "-1", "ALL");
        }
    }
}
